package com.telchina.getui.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.telchina.getui.domain.CidUidRelation;
import com.telchina.getui.mapper.CidUidRelationMapper;
import com.telchina.getui.service.CidUidRelationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: zlm
 * @Date: 2023/6/7
 * @Description: com.telchina.getui.service.impl
 * @Version: 1.0
 */
@Service
public class CidUidRelationServiceImpl extends ServiceImpl<CidUidRelationMapper, CidUidRelation> implements CidUidRelationService {

    /**
     * 根据uid获取cid
     *
     * @param uid
     * @return
     */
    @Override
    public List<String> getCidByUid(List<String> uid) {
        return baseMapper.selectList(Wrappers.<CidUidRelation>lambdaQuery().in(CidUidRelation::getUid, uid)).stream().map(CidUidRelation::getCid).collect(Collectors.toList());
    }

    /**
     * 更新cid和uid关系
     *
     * @param cid
     * @param uid
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(String cid, String uid) {
        baseMapper.delete(Wrappers.<CidUidRelation>lambdaQuery().eq(CidUidRelation::getCid, cid).or().eq(CidUidRelation::getUid, uid));
        return save(new CidUidRelation(cid, uid));
    }
}
